﻿<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Plugin.php 3985 2010-07-25 16:14:44Z huuphuoc $
 * @since		2.0.0
 */
/*
	Plugin Enterpriseinactive : 
	- purpose: cấm mọi hoạt động liên quan đến enterprise mà frontend_active = 0	
	- excute : 
		trong param có chứa enterprise_id -> kết nối DB -> tìm đối tượng EnterpriseObj theo getById
		Nếu EnterpriseObj == null -> redirect đến trang chủ
		Ngược lại -> nếu EnterpriseObj->frontend_active == 0 
						->redirect đến bizprofile/deny
*/
class Plugins_EnterpriseInActive_Plugin extends Tomato_Controller_Plugin 
{
	public function __construct() 
	{
		parent::__construct();
	}
	
	public function preDispatch(Zend_Controller_Request_Abstract $request) 
	{
        $module     = strtolower($request->getModuleName());
        $controller = strtolower($request->getControllerName());
        $action     = strtolower($request->getActionName());	
		$params     = $request->getParams();			
		
		$currentUri = $request->getRequestUri();
		$currentUri = Tomato_Utility_String::normalizeUri($currentUri);
		$currentUri = '/' . $currentUri.'/';
		if(strstr($currentUri,"/admin/")) return;


		$enterprise_id = $request->getParam('enterprise_id', false);	
		/*
		*  riêng bizprofile/Auth/login ta không check plugin
		*  (???)			
		*/
		if($module != "bizprofile" || $controller != "auth" || $action != "login"){
			if($enterprise_id){
				
				/**
				* Get connection to database
				**/
				$conn = Tomato_Db_Connection::factory()->getMasterConnection();

				/**
				* Cart models access to data
				**/
				$enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
				$enterpriseDao->setDbConnection($conn);
				$enterpriseObj = $enterpriseDao->getById($enterprise_id);
				if($enterpriseObj){
					$frontend_active = isset($enterpriseObj->frontend_active) ? $enterpriseObj->frontend_active : null;			
					if(!$frontend_active){
						$this->_request->setModuleName('bizprofile')
								->setControllerName('Enterprise')
								->setActionName('deny')
								->setDispatched(true);							
					}
				}else{				  
					  $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
					  $redirector->gotoUrl('');			
				}
			} 
		}
	}
}
